<!-- <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>完美的$.get $.post $.ajax</title>
</head>
<body>
  <script>
    var $ = {
      get : function(url,data,success,dataType){
        var xhr = this.createXhr();
        var str = url +'?'+ data;
        xhr.open('get',str);
        xhr.send();
        xhr.onload = function(){
          var result = xhr.responseText;
          if(dataType == 'json'){
            result = JSON.parse(result);
          };
          success(result);
        };
      },

      post : function(url,data,success,dataType){
        var xhr = this.createXhr();
        xhr.open('post',url);
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
        xhr.send(data);
        xhr.onload = function(){
          var result = xhr.responseText;
          if(dataType == 'json'){
            result = JSON.parse(result);
          };
          success(result);
        };
      },

      ajax : function(obj){
        var type = obj.type || 'get';
        var url = obj.url;
        var data = obj.data || '';
        var dataType = obj.dataType || 'text';
        var success = obj.success;

        if(type == 'get'){
          this.get(url,data,success,dataType);
        }else if(type == 'post'){
          this.post(url,data,success,dataType);
        };
      },

      createXhr : function(){
        var xhr = '';
        if(window.XMLHttpRequest){
          xhr = new XMLHttpRequest();
        }else{
          xhr = new ActiveXObject('Msxml2','XMLHTTP');
        };
        return xhr;
      },
    }; 

    $.get('/api/searchStudent','name=张',function(msg){
      console.log(msg);
    },'json');

    $.post('/api/postString','type=罗小黑',function(msg){
      console.log(msg)
    },'text');

    $.ajax({
      type : 'get',
      url : '/api/searchStudent',
      data : 'name=小昭',
      dataType : 'json',
      success : function(msg){
        console.log(msg)
      },
    });
  </script>
</body>
</html> -->


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>封装成 发送Ajax的方法</title>
</head>
<body>
  <script src="ajax.js"></script>
  <script>
    $.ajax({
      type : 'post',
      url : '/api/postJson',
      data : '',
      dataType : 'json',
      success : function(msg){
        console.log(msg)
      },
    });
  </script>
</body>
</html>